package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.msg.SeckillCodemsg;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefixUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class PathController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("/getPath")
    public Result getPath(String uuid,String verifyCode){
        //如果验证码不能为空  提醒用户验证码为空
        if (StringUtils.isEmpty(verifyCode)){
           throw  new BusinessException(SeckillCodemsg.VERIFYCODE_NOT_NULL);
        }
        Integer redisVerifyCode = redisService.get(SeckillKeyPrefixUtil.SECKILL_VERIFYCODE, uuid, Integer.class);
        if (!verifyCode.equals(redisVerifyCode+"")){
            throw new BusinessException(SeckillCodemsg.VERIFYCODE_ERROR);
        }
        String  path = UUID.randomUUID().toString().replaceAll("-", "");
        redisService.set(SeckillKeyPrefixUtil.SECKILL_PATH, uuid, path);
        return Result.success(path);
    }
}
